package com.lnking.lnkingplace.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lnking.lnkingplace.mapper.PlaceInfoMapper;
import com.lnking.lnkingplace.mapper.SpecialPlaceMapper;
import com.lnking.lnkingplace.model.dto.exhibition.ExhibitionStatisticDto;
import com.lnking.lnkingplace.model.dto.meeting.MeetingStatisticDto;
import com.lnking.lnkingplace.model.dto.statistic.*;
import com.lnking.lnkingplace.model.dto.statistic.cultureEnterprise.CultureEnterpriseByYearDto;
import com.lnking.lnkingplace.model.dto.statistic.cultureEnterprise.CultureEnterpriseStatisticsDto;
import com.lnking.lnkingplace.model.dto.statistic.fourEnterprise.FourEnterpriseByTypeDto;
import com.lnking.lnkingplace.model.dto.statistic.fourEnterprise.FourEnterpriseStatisticsDto;
import com.lnking.lnkingplace.model.entity.Inspect;
import com.lnking.lnkingplace.model.request.exhibition.ExhibitionStatisticSearchRequest;
import com.lnking.lnkingplace.model.request.statistic.*;
import com.lnking.lnkingplace.mapper.StatisticMapper;
import com.lnking.lnkingplace.service.IStatisticsService;
import com.lnking.starter.mybatis.entity.IPageInfo;
import com.lnking.starter.mybatis.entity.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author sn
 * @date 2022/7/14
 * @description
 */
@Service
public class StatisticsServiceImpl implements IStatisticsService {

    @Resource
    StatisticMapper statisticMapper;

    @Resource
    PlaceInfoMapper placeInfoMapper;

    @Resource
    SpecialPlaceMapper specialPlaceMapper;

    /**
     * 分号隔开
     */
    public static final String SPIT = ";";
    public static final String DIAN = "、";

    public static final String OK = "OK";
    public static final String FAIL = "FAIL";
    public static final String ALL = "合计";
    public static final String HISTORY = "history";
    public static final String NOW = "now";
    public static final String CONTROL = "control";


    /**
     * 从数据库视图 person_check_status 获取所有人员的检查信息， 视图内提供几个重要的参数信息
     * 1、diff_date 最近一次核酸检测时间距离当前时间的间隔天数
     * 2、config_day 人员对应的场所的配置，n天一检
     *
     * @param params
     */

    /**
     * 督察抽查统计
     *
     * @return
     */
    @Override
    public InspectDto inspect() {
        //   督察抽查次数
        InspectDto dto = statisticMapper.inspectNum();
        //   督察整改次数
        dto.setRectificationNum(statisticMapper.rectificationNum());
        //   已查场所总数
        dto.setPlaceNum(String.valueOf(Arrays.stream(String.join(SPIT, statisticMapper.placeAll()).split(SPIT)).count()));
        //   检查参与人数
        dto.setUserNum(String.valueOf(Arrays.stream(String.join(SPIT, statisticMapper.userAll()).split(SPIT)).count()));
        return dto;
    }

    /**
     * 人员行业分布
     *
     * @param params
     * @return
     */
    @Override
    public UserPlaceDto userPlace(UserPlaceRequest params) {
        // 1.读取场所类型
        List<String> type = placeInfoMapper.getType();
        UserPlaceDto dto = new UserPlaceDto();
        // 2.查询所有场所和所有人员
        dto.setPlaceNum(statisticMapper.placeNum(params) + statisticMapper.specialPlaceNum(params));
        dto.setUserNum(statisticMapper.userNum(params) + statisticMapper.specialUserNum(params));

        List<PlaceTypeNumDto> placeTypeNumList = new ArrayList<>();
        type.forEach(e -> {
            PlaceTypeNumDto typeNumDto = new PlaceTypeNumDto();
            typeNumDto.setTypeName(e);
            params.setPlaceType(e);
            // 3.查询场所数量写入
            typeNumDto.setPlaceNum(statisticMapper.placeNum(params));
            // 4.查询人员数量写入
            typeNumDto.setUserNum(statisticMapper.userNum(params));
            placeTypeNumList.add(typeNumDto);
        });
        // 加入特殊场所
        List<String> specialType = specialPlaceMapper.getType();

        specialType.forEach(e -> {
            PlaceTypeNumDto typeNumDto = new PlaceTypeNumDto();
            typeNumDto.setTypeName(e);
            params.setPlaceType(e);
            // 3.查询场所数量写入
            typeNumDto.setPlaceNum(statisticMapper.specialPlaceNum(params));
            // 4.查询人员数量写入
            typeNumDto.setUserNum(statisticMapper.specialUserNum(params));
            placeTypeNumList.add(typeNumDto);
        });

        // ===
        dto.setPlaceTypeNumList(placeTypeNumList);
        return dto;
    }


    @Override
    public Map<String, Integer> getTodayCheckRate(PersonCheckStatusSearchParams params) {
        if (params.getResultTime() == null) {
            params.setResultTime(LocalDateTime.now());
        }
        // 获取当日核酸检测人数
        Integer checkNumber = statisticMapper.getTodayCheckPersonNumber(params);
        // 获取需要检测核酸总人数
        Integer allPersonNum = statisticMapper.getAllPersonNum(params);

        Map<String, Integer> result = new HashMap<>();
        result.put("checkNumber", checkNumber);
        result.put("allNumber", allPersonNum);
        return result;
    }

    /**
     * 综合数据展示
     *
     * @return
     */
    @Override
    public IntegratedDto integrated() {
        IntegratedDto dto = new IntegratedDto();
        //    隔离场所专项督察
        dto.setPlaceNum(statisticMapper.leaderShipNum());
        //    重点人员关怀
        dto.setAssistanceNum(statisticMapper.assistanceNum());
        //    诉求建议处理回应
        dto.setSuggestNum(statisticMapper.suggestNum());
        //    公告通知
        dto.setNoticeNum(statisticMapper.noticeNum());
        return dto;
    }

    /**
     * 隔离人员数据
     *
     * @return
     */
    @Override
    public IsolationDto isolation() {
        IsolationDto dto = statisticMapper.isolation();
        dto.setHistoryNum(dto.getHistoryHealthyNum() + dto.getNowNum());
        return dto;
    }

    /**
     * 隔离观察房间情况
     *
     * @return
     */
    @Override
    public CommonIsolationDto isolationRoom() {
        CommonIsolationDto dto = new CommonIsolationDto();
        dto.setAllNum(statisticMapper.isolationRoom());
        dto.setCommonTypeNumDtoList(statisticMapper.getRoomByType());
        return dto;
    }

    /**
     * 隔离观察人员情况
     *
     * @return
     */
    @Override
    public CommonIsolationDto isolationUser() {
        CommonIsolationDto dto = new CommonIsolationDto();
        dto.setAllNum(statisticMapper.isolationUser());
        dto.setCommonTypeNumDtoList(statisticMapper.getUserByType());
        return dto;
    }

    /**
     * 驻点团队人员情况
     *
     * @return
     */
    @Override
    public CommonIsolationDto isolationWorker() {
        CommonIsolationDto dto = new CommonIsolationDto();
        dto.setAllNum(statisticMapper.isolationWorker());
        dto.setCommonTypeNumDtoList(statisticMapper.getWorkerByType());
        return dto;
    }

    @Override
    public void exportExcel(StatisticExportParams params, HttpServletResponse response) throws Exception {

//        List<StatisticCheckDto> exportData = personPlaceData(typeCheckDay);
//
//        ExportUtils.export(exportData, response, StatisticCheckDto.class);
    }


    @Override
    public AttractionsNumDto attractions(AttractionsRequest params) {
        AttractionsNumDto dto = new AttractionsNumDto();
        // 场所总数（暂时固定）
        dto.setPlaceNum(statisticMapper.getAttractionsPlaceNum());
        // 根据传入条件变化
        dto.setTouristNum(statisticMapper.getAttractionsTouristNum(params));

        dto.setAttractionsDtoList(statisticMapper.StatisticsAttractions(params));
        return dto;
    }

    @Override
    public CultureEnterpriseStatisticsDto cultureEnterpriseStatistics() {
        CultureEnterpriseStatisticsDto dto = new CultureEnterpriseStatisticsDto();
        List<CultureEnterpriseByYearDto> list = new ArrayList<>();
        // 获取类型数据
        List<EnterpriseTypeNumDto> data = statisticMapper.cultureEnterpriseTypeNum();
//        // 获取年份
//        List<Integer> years = statisticMapper.cultureEnterprisegetYear();

        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        // 初始化
        // 获取季度(保证数据里面有完整的一个季度)
        List<String> seasons = statisticMapper.getSeason();
        seasons.add(0,"0");
        dto.setSeasons(seasons);
        // 根据年份去拿取数据
        CultureEnterpriseByYearDto yearDto = new CultureEnterpriseByYearDto();
        List<Double> cultureEnterpriseBySeasonDtoList = new ArrayList<>();
        for (String season : seasons) {
            Double bySeason = statisticMapper.cultureEnterpriseByYear(year, season);
            cultureEnterpriseBySeasonDtoList.add(bySeason);
        }
        yearDto.setYear(year);
        yearDto.setCultureEnterpriseByQuarterDtoList(cultureEnterpriseBySeasonDtoList);
        list.add(yearDto);

        dto.setData(data);
        dto.setList(list);
        return dto;
    }

    @Override
    public FourEnterpriseStatisticsDto fourEnterpriseStatistics() {
        FourEnterpriseStatisticsDto dto = new FourEnterpriseStatisticsDto();
        List<FourEnterpriseByTypeDto> byTypeDtoList = new ArrayList<>();
        Integer num = statisticMapper.fourEnterpriseTypeNum();

        // 获取类型
        List<String> types = statisticMapper.fourEnterprisegetType();

        // 初始化
        // 获取季度(保证数据里面有完整的一个月度)
        List<String> seasons = statisticMapper.getFourSeason();
        seasons.add(0,"0");
        dto.setSeasons(seasons);

        for (String type : types) {
            FourEnterpriseByTypeDto byTypeDto = new FourEnterpriseByTypeDto();
            List<Double> byMonthDtoList = new ArrayList<>();
            for (String season : seasons) {
                Double byMonth = statisticMapper.fourEnterpriseByMonth(type, season);
                byMonthDtoList.add(byMonth);
            }
            byTypeDto.setType(type);
            byTypeDto.setList(byMonthDtoList);
            byTypeDtoList.add(byTypeDto);
        }
        dto.setNum(num);
        dto.setList(byTypeDtoList);
        return dto;
    }

    @Override
    public List<ExhibitionDto> exhibition(ExhibitionRequest params) {
        return statisticMapper.exhibition(params);
    }

    @Override
    public List<ExhibitionDto> exhibitionLast() {
        return statisticMapper.exhibitionLast();
    }

    @Override
    public Object inspectList(Page params) {
        List list;
        if (params.isPage()) {
            PageHelper.startPage(params.getPageNum(), params.getPageSize());
            list = statisticMapper.inspectList(params);
            PageInfo<Object> page = new PageInfo(list);
            return new IPageInfo(page);
        } else {
            list = statisticMapper.inspectList(params);
            return list;
        }
    }

    @Override
    public List<PlaceTypeCheckListDto> placeTypeCheckList(UserPlaceRequest params) {
        // 1.读取场所类型
        List<String> type = placeInfoMapper.getType();
        List<PlaceTypeCheckListDto> dtoList = new ArrayList<>();

        type.forEach(e -> {
            PlaceTypeCheckListDto typeNumDto = new PlaceTypeCheckListDto();
            typeNumDto.setTypeName(e);
            params.setPlaceType(e);
            // 3.查询场所数量写入
            typeNumDto.setPlaceNum(statisticMapper.placeNum(params));
            // 4.查询人员数量写入
            typeNumDto.setUserNum(statisticMapper.userNum(params));

            dtoList.add(typeNumDto);
        });
        List<PlaceTypeCheckDto> stringIntegerMap = statisticMapper.placeTypeTodayCheck(params.getStreet());
        for (PlaceTypeCheckListDto placeTypeCheckListDto : dtoList) {
            for (PlaceTypeCheckDto placeTypeCheckDto : stringIntegerMap) {
                if (placeTypeCheckDto.getTypeName().equals(placeTypeCheckListDto.getTypeName())) {
                    placeTypeCheckListDto.setTodayCheckNum(placeTypeCheckDto.getTodayCheck());
                }
            }
        }

        System.out.println(dtoList.stream().mapToInt(PlaceTypeCheckListDto::getTodayCheckNum).sum());
        //System.out.println(dtoList.stream().mapToInt(PlaceTypeCheckListDto::getUserNum).sum());
        return dtoList;
    }

    @Override
    public Object getExhibitionPlaceList(ExhibitionPlaceListRequest params) {
        List list;
        if (params.isPage()) {
            PageHelper.startPage(params.getPageNum(), params.getPageSize());
            list =  statisticMapper.getExhibitionPlaceList(params.getType());
            PageInfo<Object> page = new PageInfo(list);
            return new IPageInfo(page);
        } else {
            list =  statisticMapper.getExhibitionPlaceList(params.getType());
            return list;
        }
    }



    @Override
    public Object importantCompanyList(ExhibitionPlaceListRequest params) {
        List list;
        if (params.isPage()) {
            PageHelper.startPage(params.getPageNum(), params.getPageSize());
            if(params.getType() ==null){
                list=statisticMapper.cultureImportantCompanyList(params.getChoose());
            }else{
                list=statisticMapper.fourImportantCompanyList();
            }
            PageInfo<Object> page = new PageInfo(list);
            return new IPageInfo(page);
        } else {
            if(params.getType() ==null){
                list=statisticMapper.cultureImportantCompanyList(params.getChoose());
            }else{
                list=statisticMapper.fourImportantCompanyList();
            }
            return list;
        }
    }

    @Override
    public Object placeStreetTypeList(Page params) {
        List<StatisticCheckDto> statisticCheckDtos = this.placeTypeList();
        List list;
        if (params.isPage()) {
            PageHelper.startPage(params.getPageNum(), params.getPageSize());
                list=this.placeTypeList();
            PageInfo<Object> page = new PageInfo(list);
            IPageInfo iPageInfo = new IPageInfo(page);
            iPageInfo.setTotal(statisticCheckDtos.size());
            return iPageInfo;
        } else {
            list=  this.placeTypeList();
            return list;
        }
    }

    private List<StatisticCheckDto> placeTypeList() {
        UserPlaceRequest params = new UserPlaceRequest();
        List<StatisticCheckDto> dtoList = new ArrayList<>();
        // 1.读取场所类型
        List<String> type = placeInfoMapper.getType();
        type.forEach(e -> {
            StatisticCheckDto typeNumDto = new StatisticCheckDto();
            typeNumDto.setType(e);
            params.setPlaceType(e);
            // 3.查询场所数量写入
            typeNumDto.setPlaceNameNum(statisticMapper.placeNum(params));
            // 4.查询人员数量写入
            typeNumDto.setUserNum(statisticMapper.userNum(params));

            dtoList.add(typeNumDto);
        });
        List<PlaceTypeCheckDto> stringIntegerMap = statisticMapper.placeTypeTodayCheck(params.getStreet());
        // 今日新增
        for (StatisticCheckDto StatisticCheckDto : dtoList) {
            for (PlaceTypeCheckDto placeTypeCheckDto : stringIntegerMap) {
                if (placeTypeCheckDto.getTypeName().equals(StatisticCheckDto.getType())) {
                    StatisticCheckDto.setTodayCheckNum(placeTypeCheckDto.getTodayCheck());
                }
            }
        }

        PersonCheckStatusSearchParams searchParams = new PersonCheckStatusSearchParams();
        searchParams.setStreet(params.getStreet());
        List<PersonCheckStatusDto> personCheckStatusDtoList = statisticMapper.statisticPersonCheckStatus(searchParams);
        // 检测合格
        for (PersonCheckStatusDto placeTypeCheckDto : personCheckStatusDtoList) {
            for (StatisticCheckDto StatisticCheckDto : dtoList) {
                if (placeTypeCheckDto.getPlaceType().equals(StatisticCheckDto.getType())) {
                    if("OK".equals(placeTypeCheckDto.getCheckPassFlag())){
                        StatisticCheckDto.setCheckNum(placeTypeCheckDto.getNumber());
                        StatisticCheckDto.setConfigDay(placeTypeCheckDto.getConfigDay());
                    }else{
                        StatisticCheckDto.setCheckNum(0);
                        StatisticCheckDto.setConfigDay(placeTypeCheckDto.getConfigDay());
                    }

                }
            }
        }


//        // 体育场所特殊处理
//        List<StatisticCheckDto> threeList = dtoList.stream().filter(e -> e.getConfigDay() != null).collect(Collectors.toList());
//        sports(threeList);
        return dtoList;
    }

    @Override
    public Object inspectTrackInfo(Page params) {
        List list;
        if (params.isPage()) {
            PageHelper.startPage(params.getPageNum(), params.getPageSize());
            list=statisticMapper.inspectTrackInfo();
            PageInfo<Object> page = new PageInfo(list);
            return new IPageInfo(page);
        } else {
            list=statisticMapper.inspectTrackInfo();
            return list;
        }
    }

    private List<StatisticCheckTypeDto> statisticPersonCheckStatus(UserPlaceRequest params) {
        List<StatisticCheckTypeDto> list = new ArrayList<>();
        List<StatisticCheckDto> dtoList = new ArrayList<>();
        // 1.读取场所类型
        List<String> type = placeInfoMapper.getType();
        type.forEach(e -> {
            StatisticCheckDto typeNumDto = new StatisticCheckDto();
            typeNumDto.setType(e);
            params.setPlaceType(e);
            // 3.查询场所数量写入
            typeNumDto.setPlaceNameNum(statisticMapper.placeNum(params));
            // 4.查询人员数量写入
            typeNumDto.setUserNum(statisticMapper.userNum(params));

            dtoList.add(typeNumDto);
        });
        List<PlaceTypeCheckDto> stringIntegerMap = statisticMapper.placeTypeTodayCheck(params.getStreet());
        // 今日新增
        for (StatisticCheckDto StatisticCheckDto : dtoList) {
            for (PlaceTypeCheckDto placeTypeCheckDto : stringIntegerMap) {
                if (placeTypeCheckDto.getTypeName().equals(StatisticCheckDto.getType())) {
                    StatisticCheckDto.setTodayCheckNum(placeTypeCheckDto.getTodayCheck());
                }
            }
        }

        PersonCheckStatusSearchParams searchParams = new PersonCheckStatusSearchParams();
        searchParams.setStreet(params.getStreet());
        List<PersonCheckStatusDto> personCheckStatusDtoList = statisticMapper.statisticPersonCheckStatus(searchParams);
        // 检测合格
        for (PersonCheckStatusDto placeTypeCheckDto : personCheckStatusDtoList) {
            for (StatisticCheckDto StatisticCheckDto : dtoList) {
                if (placeTypeCheckDto.getPlaceType().equals(StatisticCheckDto.getType())) {
                    if("OK".equals(placeTypeCheckDto.getCheckPassFlag())){
                        StatisticCheckDto.setCheckNum(placeTypeCheckDto.getNumber());
                        StatisticCheckDto.setConfigDay(placeTypeCheckDto.getConfigDay());
                    }else{
                        StatisticCheckDto.setCheckNum(0);
                        StatisticCheckDto.setConfigDay(placeTypeCheckDto.getConfigDay());
                    }

                }
            }
        }


        StatisticCheckTypeDto two = new StatisticCheckTypeDto();
        two.setConfigDay("2");
        two.setAllPerson((int) dtoList.stream().filter(e -> e.getConfigDay() != null && e.getConfigDay().equals("2")).mapToDouble(StatisticCheckDto::getUserNum).sum());
        two.setCheckNum((int) dtoList.stream().filter(e -> e.getConfigDay() != null && e.getConfigDay().equals("2")).mapToDouble(StatisticCheckDto::getCheckNum).sum());
        List<StatisticCheckDto> collect = dtoList.stream().filter(e -> e.getConfigDay() != null && e.getConfigDay().equals("2")).collect(Collectors.toList());
        // 排序
        String[] twoRule = {"酒店宾馆","乡村旅游民宿","网约房","未办证留宿场所","星级乡村酒店","网吧","KTV","艺术类校外培训机构","体育类校外培训机构"};
        final List<String> twoListRule = Arrays.asList(twoRule);
        order(collect,twoListRule);

        two.setList(collect);
        list.add(two);
        StatisticCheckTypeDto three = new StatisticCheckTypeDto();
        three.setConfigDay("3");
        three.setAllPerson((int) dtoList.stream().filter(e -> e.getConfigDay() != null && e.getConfigDay().equals("3")).mapToDouble(StatisticCheckDto::getUserNum).sum());
        three.setCheckNum((int) dtoList.stream().filter(e -> e.getConfigDay() != null && e.getConfigDay().equals("3")).mapToDouble(StatisticCheckDto::getCheckNum).sum());
        List<StatisticCheckDto> threeList = dtoList.stream().filter(e -> e.getConfigDay() != null && e.getConfigDay().equals("3")).collect(Collectors.toList());

        // 体育场所特殊处理
        sports(threeList);
        // 排序
        String[] threeRule = {"A级景区","A级林盘景区","星级农家乐","美术馆","博物馆","一般体育场所","高危体育场所","乡村旅游民宿","剧本杀","农家乐","旅行社"};
        final List<String> threeListRule = Arrays.asList(threeRule);
        order(threeList,threeListRule);
        three.setList(threeList);
        list.add(three);
        return list;

    }

    @Override
    public List<StatisticCheckTypeDto> personPlaceData(UserPlaceRequest params) {
        List<StatisticCheckTypeDto> list = new ArrayList<>();
        List<StatisticCheckDto> dtoList = new ArrayList<>();
        // 1.读取场所类型
        List<String> type = placeInfoMapper.getType();
        // 2.查询所有场所和所有人员
        type.forEach(e -> {
            StatisticCheckDto typeNumDto = new StatisticCheckDto();
            typeNumDto.setType(e);
            params.setPlaceType(e);
            // 3.查询场所数量写入
            typeNumDto.setPlaceNameNum(statisticMapper.placeNum(params));
            // 4.查询人员数量写入
            typeNumDto.setUserNum(statisticMapper.userNum(params));

            dtoList.add(typeNumDto);
        });
        List<PlaceTypeCheckDto> stringIntegerMap = statisticMapper.placeTypeTodayCheck(params.getStreet());
        // 今日新增
        for (StatisticCheckDto StatisticCheckDto : dtoList) {
            for (PlaceTypeCheckDto placeTypeCheckDto : stringIntegerMap) {
                if (placeTypeCheckDto.getTypeName().equals(StatisticCheckDto.getType())) {
                    StatisticCheckDto.setTodayCheckNum(placeTypeCheckDto.getTodayCheck());
                }
            }
        }

        PersonCheckStatusSearchParams searchParams = new PersonCheckStatusSearchParams();
        searchParams.setStreet(params.getStreet());
        List<PersonCheckStatusDto> personCheckStatusDtoList = statisticMapper.statisticPersonCheckStatus(searchParams);
        // 检测合格
        for (PersonCheckStatusDto placeTypeCheckDto : personCheckStatusDtoList) {
        for (StatisticCheckDto StatisticCheckDto : dtoList) {
                if (placeTypeCheckDto.getPlaceType().equals(StatisticCheckDto.getType())) {
                    if("OK".equals(placeTypeCheckDto.getCheckPassFlag())){
                        StatisticCheckDto.setCheckNum(placeTypeCheckDto.getNumber());
                        StatisticCheckDto.setConfigDay(placeTypeCheckDto.getConfigDay());
                    }else{
                        StatisticCheckDto.setCheckNum(0);
                        StatisticCheckDto.setConfigDay(placeTypeCheckDto.getConfigDay());
                    }

                }
            }
        }


        StatisticCheckTypeDto two = new StatisticCheckTypeDto();
        two.setConfigDay("2");
        two.setAllPerson((int) dtoList.stream().filter(e -> e.getConfigDay() != null && e.getConfigDay().equals("2")).mapToDouble(StatisticCheckDto::getUserNum).sum());
        two.setCheckNum((int) dtoList.stream().filter(e -> e.getConfigDay() != null && e.getConfigDay().equals("2")).mapToDouble(StatisticCheckDto::getCheckNum).sum());
        two.setTodayCheckNum((int) dtoList.stream().filter(e -> e.getConfigDay() != null && e.getConfigDay().equals("2")).mapToDouble(StatisticCheckDto::getTodayCheckNum).sum());
        List<StatisticCheckDto> collect = dtoList.stream().filter(e -> e.getConfigDay() != null && e.getConfigDay().equals("2")).collect(Collectors.toList());
        // 排序
        String[] twoRule = {"酒店宾馆","乡村旅游民宿","网约房","未办证留宿场所","星级乡村酒店","网吧","KTV","艺术类校外培训机构","体育类校外培训机构"};
        final List<String> twoListRule = Arrays.asList(twoRule);
        order(collect,twoListRule);

        two.setList(collect);
        list.add(two);
        StatisticCheckTypeDto three = new StatisticCheckTypeDto();
        three.setConfigDay("3");
        three.setAllPerson((int) dtoList.stream().filter(e -> e.getConfigDay() != null && e.getConfigDay().equals("3")).mapToDouble(StatisticCheckDto::getUserNum).sum());
        three.setTodayCheckNum((int) dtoList.stream().filter(e -> e.getConfigDay() != null && e.getConfigDay().equals("3")).mapToDouble(StatisticCheckDto::getTodayCheckNum).sum());
        three.setCheckNum((int) dtoList.stream().filter(e -> e.getConfigDay() != null && e.getConfigDay().equals("3")).mapToDouble(StatisticCheckDto::getCheckNum).sum());
        List<StatisticCheckDto> threeList = dtoList.stream().filter(e -> e.getConfigDay() != null && e.getConfigDay().equals("3")).collect(Collectors.toList());

        // 体育场所特殊处理
        sports(threeList);
        // 排序
        String[] threeRule = {"A级景区","A级林盘景区","星级农家乐","美术馆","博物馆","一般体育场所","高危体育场所","乡村旅游民宿","剧本杀","农家乐","旅行社"};
        final List<String> threeListRule = Arrays.asList(threeRule);
        order(threeList,threeListRule);
        three.setList(threeList);
        list.add(three);
        return list;


    }
    public  void  sports(List<StatisticCheckDto> threeList){
        // 找到一般和高危险场所进行合并
        // 一般
        List<String> ordinaryPlaceList = statisticMapper.getOrdinaryPlace();
        List<StatisticCheckDto> ordinaryPlaceDto =new ArrayList<>();
        for (String ordinaryPlace : ordinaryPlaceList) {
            assert false;
            List<StatisticCheckDto> statisticCheckDtos = threeList.stream().filter(e -> e.getType().equals(ordinaryPlace)).collect(Collectors.toList());
            ordinaryPlaceDto.addAll(statisticCheckDtos);
        }

        threeList.removeAll(ordinaryPlaceDto);
        StatisticCheckDto ordinary =new StatisticCheckDto();
        ordinary.setType("一般体育场所");
        ordinary.setCheckNum(ordinaryPlaceDto.stream().mapToInt(StatisticCheckDto::getCheckNum).sum());
        ordinary.setConfigDay("3");
        ordinary.setUserNum(ordinaryPlaceDto.stream().mapToInt(StatisticCheckDto::getUserNum).sum());
        ordinary.setPlaceNameNum(ordinaryPlaceDto.stream().mapToInt(StatisticCheckDto::getPlaceNameNum).sum());
        ordinary.setTodayCheckNum(ordinaryPlaceDto.stream().mapToInt(StatisticCheckDto::getTodayCheckNum).sum());
        threeList.add(ordinary);

        // 特殊场所
        List<String> specialPlaceList = statisticMapper.getSpecialPlace();

        List<StatisticCheckDto> specialPlaceDto =new ArrayList<>();
        for (String specialPlace : specialPlaceList) {
            assert false;
            specialPlaceDto.addAll(threeList.stream().filter(e->e.getType().equals(specialPlace)).collect(Collectors.toList()));
        }
        threeList.removeAll(specialPlaceDto);
        StatisticCheckDto special =new StatisticCheckDto();
        special.setType("高危体育场所");
        special.setCheckNum(specialPlaceDto.stream().mapToInt(StatisticCheckDto::getCheckNum).sum());
        special.setConfigDay("3");
        special.setUserNum(specialPlaceDto.stream().mapToInt(StatisticCheckDto::getUserNum).sum());
        special.setPlaceNameNum(specialPlaceDto.stream().mapToInt(StatisticCheckDto::getPlaceNameNum).sum());
        special.setTodayCheckNum(specialPlaceDto.stream().mapToInt(StatisticCheckDto::getTodayCheckNum).sum());
        threeList.add(special);

    }

    public void order(List<StatisticCheckDto> dto,List<String> rule){
        Collections.sort(dto, new Comparator<StatisticCheckDto>() {
            @Override
            public int compare(StatisticCheckDto o1, StatisticCheckDto o2) {
                int io1 = rule.indexOf(o1.getType());
                int io2 = rule.indexOf(o2.getType());
                return io1 - io2;
            }
        });

    }

    /**
     * 历史总人数
     * 今日新增加
     * 管控人员
     *
     * @return
     */
    @Override
    public Map<String, Integer> outsiders(BaseStatisticParams params) {
        Map<String, Integer> data = new HashMap<>();
        // 历史总人数
        Integer historyOutsidersNum = statisticMapper.historyOutsidersNum(params);
        data.put(HISTORY, historyOutsidersNum);
        // 今日新增加
        Integer nowOutsidersNum = statisticMapper.nowOutsidersNum(params);
        data.put(NOW, nowOutsidersNum);
        // 管控人员
        Integer controlOutsidersNum = statisticMapper.controlOutsidersNum(params);
        data.put(CONTROL, controlOutsidersNum);
        return data;
    }

    /**
     * 今日督察主题
     * 参与人数
     * 检查场所数
     * 发现问题数量
     * 整改问题数量
     * top3
     *
     * @param params
     * @return
     */
    @Override
    public InspectStatisticDto inspectStatistic(InspectStatisticParams params) {
        InspectStatisticDto data = new InspectStatisticDto();
        List<String> theme = new ArrayList<>();
        long groupNum = 0;
        long placeNum = 0;
        List<Inspect> inspectList = statisticMapper.inspect(params);
        // 今日督察主题
        for (Inspect inspect : inspectList) {
            // 今日督察主题
            theme.addAll(Arrays.asList(inspect.getType().split(SPIT)));
            // 参与组数
            groupNum += Arrays.stream(inspect.getGroupNames().split(SPIT)).count();
            // 检查场所数
            placeNum += Arrays.stream(inspect.getPlaceIds().split(SPIT)).count();
        }
        // 发现问题数量
        Integer problemNum = statisticMapper.inspectResultNum(params);

        // top3
        List<String> top3 = statisticMapper.inspectTop3(params);
        data.setTheme(theme.stream().distinct().collect(Collectors.joining("、")));
        data.setGroupNum(groupNum);
        data.setPlaceNum(placeNum);
        data.setProblemNum(problemNum);
        // 未整改问题数量
        data.setRectificationNum(statisticMapper.RectificationNum(params));
        data.setList(top3);

        return data;
    }

    /**
     * 获取会展企业的数据和举办的会议/展览活动的相关数据
     *
     * @return map
     */
    @Override
    public Map<String, Object> getExhibitionStatisticInfo(ExhibitionStatisticSearchRequest params) {
        Map<String, Object> result = new HashMap<>();
        // 1、中国西部国际博览城重大展会活动
        Map<String, Object> exhibitionAndMeetingPlaceNum = statisticMapper.getExhibitionAndMeetingPlaceNum(params.getExhibitionStatisticYear());

        result.put("exhibitionPlaceInfo", exhibitionAndMeetingPlaceNum);

        // 2、外围

        Map<String, Object> peripheryPlaceNum = statisticMapper.getPeripheryPlaceNum(params.getPeripheryStatisticYear());

        result.put("peripheryPlaceInfo", peripheryPlaceNum);


        return result;
    }

}
